import Vue from 'vue';
import Router from 'vue-router';
import store from '../store/index'

// 消息模块
import message from './message';
import my from './my';
import home from './home';
import login from './login'
Vue.use(Router);

const RouterModel = new Router({
    routes: [...home, ...message, ...my, ...login],
    // mode: 'history',
    scrollBehavior(to, from, savedPosition) {
        return {
            x: 0,
            y: 0
        }
    }
});

//解决多次点击当前tabbar 路由报错问题
const originalPush = Router.prototype.push
Router.prototype.push = function push(location) {
    return originalPush.call(this, location).catch(err => err)
}



RouterModel.beforeEach((to, from, next) => {
    // 用户登录判断 
    if (to.name !== "login") {
        if (window.localStorage.userInfo) {
            next()
        } else {
            if (to.name === "register") {
                next()
            } else {
                next("/login")
            }
        }
    } else {
        if (window.localStorage.userInfo) {
            next("/home");
        } else {
            next();
        }
    }
});

export default RouterModel;